<template>
	<view>
		<list-filter :tabs="tabs" :curIndex="curTabIndex" :isAvg="true" @onChange="changeTab"></list-filter>
		<order-list-item ref="orders" :orders="orders"></order-list-item>
	</view>
</template>

<script>
	import listMall from '@/components/devm/index/list-mall.vue'
	import listFilter from '@/components/devm/list-filter.vue'
	import orderListItem from '@/components/devm/order/order-list-item.vue'
	
	export default {
		components:{
			listMall,listFilter,orderListItem
		},
		onLoad(options) {
			this.curTabIndex = options.cur || 0;
			this.initData('all');
		},
		onShow() {
			var that = this;
			uni.getStorageInfo({
				success(res) {
					console.log(res)
					var keys = res.keys;
					for(var index=0;index<keys.length;index++) {
						if(keys[index].indexOf('order_')>=0) {
							var orderno = keys[index].split('_')[1];
							for(var k=0;k<that.orders.length;k++) {
								var item = that.orders[k];
								if(item.orderno == orderno) {
									var type = uni.getStorageSync(keys[index]);
									if(type=='payed') {
										item.status = 19;//已付款
										// that.$set(that.orders,k,item);
										that.$refs.orders[index] = item;
										break;
									}
									if(type=='cancled') {
										item.status = 11;//已取消
										that.$set(that.orders,k,item);
										that.$refs.orders[index] = item;
										break;
									}
									if(type=='received') {
										item.status = 40;//已收货
										that.$set(that.orders,k,item);
										that.$refs.orders[index] = item;
										break;
									}
								}
							}
							uni.removeStorage({
								key:keys[index]
							})
						}
					}
				}
			})
		},
		data(){
			return {
				curTabIndex:0,
				tabs:['全部','待付款','待收货','待评价','已完成'],
				orders:[]
			}
		},
		methods:{
			initData(type) {
				var that = this;
				this.orders = [];
				this.$get('mall/order/list', {type:type}).then((res) => {
					if (res.data) {
						this.orders = res.data;
					}
				})
			},
			changeTab(index) {
				this.curTabIndex = index;
				console.log('index -> '+index);
				var type = 'all'
				if(index==1) {
					type = 'pay';
				} else if(index==2) {
					type = 'receive';
				} else if(index==3) {
					type = 'comment';
				} else if(index==4) {
					type = 'finish';
				}
				this.initData(type);
			}
		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}
</style>
